<?php
include(dirname(__FILE__).'/../../../includes.php5');

include_once($project_path.'/class/user/user.class.php5');
session_start();
include_once($project_path.'/class/fw/dictionary.class.php5');

userUser::checkSession();

$action=(isset($_GET['action'])?$_GET['action']:'');
switch($action)
{
	case 'refeshlogin':
		if($_SESSION['current_user']->isAnonymous())
		{
			$_SESSION['current_user'] = userUser::autoLogin();
		}
		$_SESSION['current_user']->displayJS();
		echo 'userPageDisplay(new Array("«HOME_PROFIL»", "«HOME_LOGOUT»", "«HOME_LOGIN»", "«HOME_REGISTER»"));';
		if($_SESSION['current_user']->isRoot())
		{
			echo 'adminUpdateCheck();';
		}
		break;
	case 'profil':
		if($_SESSION['current_user']->isAnonymous())
		{
			echo 'profil_popup.hide();';
			die();
		}
		include_once($project_path.'/class/fw/smarty.class.php5');
		$smarty = new fwSmarty();
		$smarty->assign('user', $_SESSION['current_user']);
		$h = opendir($project_path.'/lang/');
		$langs = array();
		while($f = readdir($h))
		{
			if($f!='.' && $f!='..' && is_dir($project_path.'/lang/'.$f) && file_exists($project_path.'/lang/'.$f.'/index.php5'))
			{
				include($project_path.'/lang/'.$f.'/index.php5');
				$langs[$f] = $lang_info;
				if(!is_file($project_path.'/view/css/user/img/flag_'.$f.'.gif'))
				{
					if(isset($lang_info['flag']) && is_file($project_path.'/lang/'.$f.'/'.$lang_info['flag']))
					{
						copy($project_path.'/lang/'.$f.'/'.$lang_info['flag'], $project_path.'/view/css/user/img/flag_'.$f.'.gif');
					}
				}
			}
		}
		$smarty->assign('languages', $langs);
		$smarty->assign('extrat', is_dir($project_path.'/view/extrat/avatar/64'));
		
		echo 'profil_popup.show("'.fwConverter::text2JSString($smarty->getContents('user/profil.tpl')).'");
userPasswordMessages = new Array(\'«GLOBAL_PASSWORD_LEVEL_LOW»\', \'«GLOBAL_PASSWORD_LEVEL_NORMAL»\', \'«GLOBAL_PASSWORD_LEVEL_HIGHT»\', \'«GLOBAL_PASSWORD_LEVEL_HIGHEST»\')
userDisplayProfil();';
		break;
	case 'list_avatar':
		if(is_dir($project_path.'/view/extrat/avatar/64'))
		{
			$h = opendir($project_path.'/view/extrat/avatar/64');
			echo 'document.getElementById("user_profil_avatar_content").innerHTML=\'';
			while($f = readdir($h))
			{
				if($f=='.' || $f=='..' || $f=='000.png') continue;
				if(!is_file($project_path.'/view/extrat/avatar/64/'.$f)) continue;
				echo '<img id="avatar_'.$f.'" src="./extrat/avatar/64/'.$f.'"> \\
	';
			}
			echo '\';';
		}
		break;
	case 'update':
		if($_SESSION['current_user']->isAnonymous())
		{
			die();
		}
		$_SESSION['current_user']->loadPerson();
		$_SESSION['current_user']->person->name = (isset($_POST['txt_name'])?$_POST['txt_name']:'');
		$_SESSION['current_user']->person->firstName = (isset($_POST['txt_firstname'])?$_POST['txt_firstname']:'');
		$_SESSION['current_user']->person->email = (isset($_POST['txt_email'])?$_POST['txt_email']:'');
		$_SESSION['current_user']->person->save();
		if(in_array((isset($_POST['txt_language'])?$_POST['txt_language']:''), array('fr', 'gb')))
		{
			$_SESSION['current_user']->language = $_POST['txt_language'];
		}
		$_SESSION['current_user']->avatarColor = (isset($_POST['txt_color'])?$_POST['txt_color']:'');
		$_SESSION['current_user']->avatar = (isset($_POST['txt_avatar'])?$_POST['txt_avatar']:'');
		$_SESSION['current_user']->save();
		echo 'current_user.person.name = "'.fwConverter::text2JSString($_SESSION['current_user']->person->name).'";
current_user.person.firstName = "'.fwConverter::text2JSString($_SESSION['current_user']->person->firstName).'";
current_user.language = "'.fwConverter::text2JSString($_SESSION['current_user']->language).'";
current_user.color = "#'.fwConverter::text2JSString($_SESSION['current_user']->avatarColor).'";
current_user.avatar = "'.fwConverter::text2JSString($_SESSION['current_user']->avatar).'.png";
userPageDisplay(new Array("«HOME_PROFIL»", "«HOME_LOGOUT»", "«HOME_LOGIN»", "«HOME_REGISTER»"));
';
		break;
}
?>